﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace VAdvantage.Controls
{
    public class VLabel : Label,IControl
    {

        private const long serialVersionUID = 1912498227823474678L;

        private Control decorator;

        private string _columnName = "";

        private bool mandatory;

        public VLabel():this(HorizontalAlignment.Right)
        {
        }

        public VLabel(HorizontalAlignment hor)
        {
            this.HorizontalAlignment =  hor;
        }


        public VLabel(String value,string name)
            : base()
    {
        this.Content = value != null ? value.Replace("[&]", "") : "";
        SetName(name);
    }

        public VLabel(String value)
            : base()
        {
            this.Content = value != null ? value.Replace("[&]", "") : "";
        }

        public bool IsMandatory()
        {
            return mandatory;
        }

        public void SetMandatory(bool mandatory)
        {
            this.mandatory = mandatory;
            SetupMandatoryDecorator();
        }

        public Control GetDecorator()
        {
            return decorator;
        }

        public void SetValue(String value)
        {
            this.Content = value != null ? value.Replace("[&]", "") : "";
            if ((value == null || value.Trim().Length == 0) && decorator != null)
                decorator.Visibility = System.Windows.Visibility.Collapsed;
        }


        public bool SetVisible(bool visible)
        {
            if (decorator != null)
            {
                if (visible)
                    decorator.Visibility = (GetValue() != null && GetValue().ToString().Trim().Length > 0 && mandatory) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
                else
                    decorator.Visibility = System.Windows.Visibility.Collapsed;
            }
            this.Visibility = visible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            return visible;
        }

        public object GetValue()
        {
            return this.Content;
        }


        private void SetupMandatoryDecorator()
        {
            if (decorator == null)
                CreateMandatoryDecorator();
            String value = GetValue().ToString();
            if (mandatory && value != null && value.Trim().Length > 0) {
                ((VLabel)decorator).SetVisible(true);
            } else
                ((VLabel)decorator).SetVisible(false);
        }

        private void CreateMandatoryDecorator()
        {
            decorator = new VLabel();
            ((Label)decorator).Content = "*";
            //((Label)decorator).Style. .setSclass("mandatory-decorator-text");
        }

       
        public void SetText(String translate)
        {
            if (translate != null)
                translate = translate.Replace("[&]", "");
            this.SetValue(translate);
        }

        public Control RightAlign()
        {
            this.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;// LayoutUtils.makeRightAlign(this);
            return this;
        }

        public event EventHandler VetoableChangeListener;

        public int GetDisplayType()
        {
            return Classes.DisplayType.Label;
        }

        public void SetDisplayType(int displayType)
        {
           //
        }

        public bool GetIsReadonly()
        {
            throw new NotImplementedException();
        }

        public void SetReadOnly(bool readOnly)
        {
            throw new NotImplementedException();
        }

        public Grids.GridField GetField()
        {
            throw new NotImplementedException();
        }

        public void SetField(Grids.GridField mField)
        {
            throw new NotImplementedException();
        }

        public string GetName()
        {
            return  this._columnName;
        }

        public void SetName(string name)
        {
            _columnName = "lbl"+name;
        }

        public string GetText()
        {
          return  (string) this.Content;
        }

        public void SetValue(object value)
        {
            
        }

        public bool GetIsMandatory()
        {
            throw new NotImplementedException();
        }

        public void SetIsMandatory(bool value)
        {
            throw new NotImplementedException();
        }

        public void SetBackGround(bool value)
        {
            throw new NotImplementedException();
        }

        public string GetSelectedText()
        {
            throw new NotImplementedException();
        }

        public bool IsDisabled()
        {
            throw new NotImplementedException();
        }

        public void SetDisabled(bool disabled)
        {
            throw new NotImplementedException();
        }

        public int GetTabIndex()
        {
            throw new NotImplementedException();
        }

        public void SetTabindex(int tabindex)
        {
            throw new NotImplementedException();
        }

        public bool Multiline
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool Visible
        {
            get
            {
                return this.Visibility == System.Windows.Visibility.Visible;
            }
            set
            {
                SetVisible(value);
            }
        }

        public bool Focused
        {
            get { throw new NotImplementedException(); }
        }

        public string BindValue
        {
            set { throw new NotImplementedException(); }
        }

        public void Dispose()
        {
            decorator = null;
        }
    }
}
